import React from 'react';
import * as api from '../stores/api';
import { TreeSelector } from '../../components';

const DictItemTreeSelector = ({ dictId, ...props }) => {
  const mapper = (items) => items.map((d) => ({ key: d.fullId, label: d.name }));
  return (
    <TreeSelector
      {...props}
      getTopItems={(_) => api.getTopItems(dictId).then(mapper)}
      getChildren={(t, fullId) => {
        const pId = fullId.substr(fullId.lastIndexOf(':') + 1);
        return api.getChildren(dictId, pId).then(mapper);
      }}
    />);
};

export default DictItemTreeSelector;
